package com.elin4it.ssm.util;
import com.elin4it.ssm.pojo.Answer;
import com.elin4it.ssm.pojo.GameRoom;
import com.elin4it.ssm.pojo.UserAnswer;
import com.elin4it.ssm.pojo.Userinfo;
import com.elin4it.ssm.service.AnswerService;
import com.elin4it.ssm.service.GameRoomService;
import com.elin4it.ssm.service.UserAnswerService;
import com.elin4it.ssm.service.UserinfoService;

import java.util.*;
public class TimerTaskTest03 extends TimerTask {
    @Override
    public void run() {
        UserinfoService userinfoService=(UserinfoService) ApplicationContextUtil.gerBean("UserinfoService");
        GameRoomService gameRoomService=(GameRoomService) ApplicationContextUtil.gerBean("GameRoomService");
        AnswerService answerService=(AnswerService)ApplicationContextUtil.gerBean("AnswerService");
        UserAnswerService userAnswerService= (UserAnswerService) ApplicationContextUtil.gerBean("UserAnswerService");
        List<Userinfo> list=userinfoService.selectRival();//查询等待用户状态的信息
        List<Userinfo> newList=new ArrayList<Userinfo>();
        List<Answer> answersList=new ArrayList<Answer>();
        List<Answer> answerList=new ArrayList<Answer>();
        if(list.size()>=2)
        {
            newList=userinfoService.getRandomList(list,2);//随机取两个用户的信息
            answersList=answerService.selectAll();//总的题目信息
            answerList=userinfoService.getRandomList(answersList,5); //随机抽取5道题目的编号，并且插入到所在的房间中
            int roomId=gameRoomService.selectByRoomId();//获取房间号
            GameRoom gameRoom=new GameRoom();//插入数据到game_room,并且标识了两人在同一个房间，并且把房间的状态开通（1），最后要关闭（0）!
            gameRoom.setRoomId(roomId);
            gameRoom.setOpenid1(newList.get(0).getOpenid());
            gameRoom.setOpenid2(newList.get(1).getOpenid());
            gameRoom.setAnswerId1(answerList.get(0).getAnswerId());
            gameRoom.setAnswerId2(answerList.get(1).getAnswerId());
            gameRoom.setAnswerId3(answerList.get(2).getAnswerId());
            gameRoom.setAnswerId4(answerList.get(3).getAnswerId());
            gameRoom.setAnswerId5(answerList.get(4).getAnswerId());
            gameRoom.setStatus(1);
            gameRoomService.insert(gameRoom);
            userinfoService.updateStatusToOne(newList.get(0).getOpenid());//改变两人的状态为竞赛状态（1）
            userinfoService.updateStatusToOne(newList.get(1).getOpenid());
            UserAnswer userAnswer=new UserAnswer();
            userAnswer.setRoomId(roomId);//插入信息到useranswer表
            userAnswer.setScore(0);
            userAnswer.setOpenid(newList.get(0).getOpenid());
            userAnswerService.insertSelective(userAnswer);
            UserAnswer userAnswer1=new UserAnswer();
            userAnswer1.setRoomId(roomId);//插入信息到useranswer表
            userAnswer1.setOpenid(newList.get(1).getOpenid());
            userAnswer1.setScore(0);
            userAnswerService.insertSelective(userAnswer1);
        }
        else
        {

        }
    }
}
